package edu.controller;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import edu.terapia.model.Persona;
import edu.terapia.model.TipoPersona;
import edu.util.HbnUtil;

@ManagedBean(name="personalBean")
@SessionScoped
public class PersonaBean implements Serializable {

	public Persona persona;
	public List<Persona> perList = null;
	public List<TipoPersona> tipoPersonas = null;
	public TipoPersona tipoPersona = null;
	public static final int TIPO_PERSONA_TERAPEUTA_ID = 100;
	
	public Persona getPersona() {
		return persona;
	}
	public void setPersona(Persona persona) {
		this.persona = persona;
	}
	
	public TipoPersona getTipoPersona() {
		return tipoPersona;
	}
	public void setTipoPersona(TipoPersona tipoPersona) {
		this.tipoPersona = tipoPersona;
	}

	public List<Persona> getPerList() {
		return perList;
	}
	
	public List<TipoPersona> getTipoPersonas() {
		return tipoPersonas;
	}
	
	@SuppressWarnings("unchecked")
	public PersonaBean() {
		persona = new Persona();
		tipoPersona = new TipoPersona();
		
		SessionFactory s =  HbnUtil.getSessionFactory();
		Transaction t = s.getCurrentSession().beginTransaction();
		perList = getCustomPersonaList(s);
		tipoPersonas = getCustomTiposPersona(s);
		t.commit();
		s.close();
	}
	
	public String save() {
		persona.setTipoPersona(tipoPersona);
		SessionFactory s =  HbnUtil.getSessionFactory();
		Transaction t = s.getCurrentSession().beginTransaction();
		
		s.getCurrentSession().save(persona);
		perList = getCustomPersonaList(s);
		t.commit();
		s.close();
		return "personal?faces-redirect=true";
	}
	
	public String update() {
		persona.setTipoPersona(tipoPersona);
		SessionFactory s =  HbnUtil.getSessionFactory();
		Transaction t = s.getCurrentSession().beginTransaction();
		s.getCurrentSession().update(persona);
		perList = getCustomPersonaList(s);
		t.commit();
		s.close();
		return "personal?faces-redirect=true";
	}
	
	public String edit(Persona persona) {
		this.persona = persona;
		this.tipoPersona = persona.getTipoPersona();
		return "update_personal?faces-redirect=true";
	}
	
	public String delete(Persona persona) {
		this.persona = persona;
		persona.setTipoPersona(tipoPersona);
		SessionFactory s =  HbnUtil.getSessionFactory();
		Transaction t = s.getCurrentSession().beginTransaction();
		s.getCurrentSession().delete(persona);
		perList = getCustomPersonaList(s);
		t.commit();
		s.close();
		return "personal?faces-redirect=true";
	}
	
	public void clear() {
		persona = new Persona();
		tipoPersona = new TipoPersona();
	}
	
	@SuppressWarnings("unchecked")
	private List<Persona> getCustomPersonaList(SessionFactory s) {
		return s.getCurrentSession()
				.createCriteria(Persona.class)
				.add(Restrictions.ne("tipoPersona.id",
						TIPO_PERSONA_TERAPEUTA_ID)).addOrder(Order.desc("id"))
				.list();
	}
	
	private List<TipoPersona> getCustomTiposPersona(SessionFactory s) {
		return (List<TipoPersona>) s.getCurrentSession()
				.createCriteria(TipoPersona.class)
				.add(Restrictions.ne("id", TIPO_PERSONA_TERAPEUTA_ID)).list();
	}

}
